import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_share/apis/home.dart';
import 'package:flutter_share/pages/home/components/home_list.dart';

import 'package:flutter_share/pages/home/components/home_nav.dart';
import 'package:flutter_share/utils/RequestDio.dart';
import 'package:flutter_share/utils/toast.dart';
import 'package:flutter_share/utils/token_manager.dart';

class HomeIndex extends StatefulWidget {
  const HomeIndex({super.key});

  @override
  // ignore: library_private_types_in_public_api
  _HomeIndexState createState() => _HomeIndexState();
}

class _HomeIndexState extends State<HomeIndex> {
  // 定时器实例
  Timer? timer;
  // 倒计时秒数
  int countDown = 5;
  // 获取数据列表
  List announcementList = [];

  @override
  void initState() {
    super.initState();
    getAnnouncementList();
  }

  // 获取社区公告
  getAnnouncementList() async {
    // 请求社区公告列表
    final res = await HomeApi.getAnnouncementList();
    // PromptAction.showSuccess(res.toString());
    // 更新社区公告列表
    setState(() {
      announcementList = res;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('享+社区'),
        centerTitle: true,
        backgroundColor: Colors.transparent,
      ),
      backgroundColor: const Color(0xffd2dde8),
      body: ListView(
        children: [
          // 添加点击事件
          Row(
            children: [
              TextButton(
                onPressed: () async {
                  //final res = await requestDio.get('/announcement');
                  final res = await requestDio.get('/house');
                  print(res);
                },
                child: const Text('测试网络请求'),
              ),
              TextButton(
                onPressed: () async {
                  final res = await HomeApi.getAnnouncementList();
                  print(res);
                },
                child: const Text('测试网络请求get'),
              ),
              TextButton(
                onPressed: () {
                  // 弹窗
                  PromptAction.showSuccess('成功');
                },
                child: const Text('测试弹窗'),
              ),
            ],
          ),
          Row(
            children: [
              TextButton(
                onPressed: () {
                  // 自定义弹窗
                  showDialog(
                      context: context,
                      // AlertDialog 警告弹窗 模态
                      builder: (context) => AlertDialog(
                            // 自定义弹窗内容
                            title: const Text('警告弹窗'), // 标题
                            content: const Text('测试弹窗内容'), // 内容
                            // 按钮组
                            actions: [
                              TextButton(
                                  onPressed: () {
                                    // 关闭弹窗
                                    Navigator.of(context).pop();
                                  },
                                  child: const Text('取消')),
                              TextButton(
                                  onPressed: () {}, child: const Text('确定'))
                            ],
                          ));
                },
                child: const Text('flutter警告弹窗'),
              ),
              TextButton(
                  onPressed: () {
                    // 自定义弹窗
                    showDialog(
                        context: context,
                        // DatePickerDialog 日历弹窗 模态
                        builder: (context) => DatePickerDialog(
                              firstDate: DateTime(1900), // 最小日期
                              lastDate: DateTime(2050), // 最大日期
                              initialDate: DateTime.now(), // 初始日期
                            ));
                  },
                  child: const Text("flutter日历弹窗"))
            ],
          ),
          // 本地化存储
          Row(
            children: [
              TextButton(
                onPressed: () {
                  tokenManager.setToken('1234567890', 'asdfghjkl');
                },
                child: const Text('持久化存储-存'),
              ),
              TextButton(
                onPressed: ()  {
                  final token =  tokenManager.getToken();
                  PromptAction.showSuccess(token);
                },
                child: const Text('取token'),
              ),
              TextButton(
                onPressed: ()  {
                  final token =  tokenManager.getRefreshToken();
                  PromptAction.showSuccess(token);
                },
                child: const Text('取刷新token'),
              ),
              TextButton(
                onPressed: () {
                  tokenManager.deleteToken();
                },
                child: const Text('删token'),
              ),
            ],
          ),
          // 定时器
          Row(
            children: [
              TextButton(
                  onPressed: () {
                    timer = Timer.periodic(const Duration(seconds: 1), (timer) {
                      setState(() {
                        countDown--;
                        if (countDown < 0) {
                          timer.cancel();
                          countDown = 5;
                        }
                      });
                    });
                  },
                  child: const Text("开始")),
              Text('秒数:$countDown'),
              TextButton(
                  onPressed: () {
                    timer?.cancel();
                  },
                  child: const Text("停止"))
            ],
          ),
          const HomeNav(),
          HomeList(notifyList: announcementList),
          // 定时器
          const SizedBox(height: 100),
        ],
      ),
    );
  }
}
